/*
Copyright (C) 2008, 2009 Henrik Naess

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Diagnostics;
using System.Globalization;
using System.Windows.Forms;
using MatrixPlugin.Utils;
using ZoneFiveSoftware.Common.Visuals;
using GpsRunningPlugin.Util;

namespace MatrixPlugin
{
    public partial class SettingsUC : UserControl
    {
        private readonly SettingsController _settingsController = SettingsController.Instance();

        public SettingsUC()
        {
            InitializeComponent();

            UpdateUIFromPreferences();
        }

        private void UpdateUIFromPreferences()
        {
            presentRadius();
            this.txtBoxToolTip.TextChanged -= new System.EventHandler(this.textBoxToolTip_TextChanged);
            txtBoxToolTip.Text = Settings.ToolTipFormatString;
            this.txtBoxToolTip.TextChanged += new System.EventHandler(this.textBoxToolTip_TextChanged);
        }

        public void ThemeChanged(ZoneFiveSoftware.Common.Visuals.ITheme visualTheme)
        {
            this.PluginInfoPanel.ThemeChanged(visualTheme);
            this.toleranceForLength.ThemeChanged(visualTheme);
            this.txtBoxToolTip.ThemeChanged(visualTheme);
        }

        public void UICultureChanged(CultureInfo culture)
        {
            lblTitle.Text = string.Format(Properties.Resources.MatrixVersion, Properties.Resources.ApplicationName, (new Plugin()).Version);

            lblTrails.Text = _settingsController.TrailsPlugin.CompabilityText;
            this.lblUniqueRoutes.Text = TrailsPlugin.Integration.UniqueRoutes.CompabilityText;

            lblFileName.Text = Constants.Preferences.FullFileName;
            this.lblLicense.Text = Properties.Resources.lblLicense;
            this.lblToolTipExplanation.Text = string.Format(Properties.Resources.lblToolTipExplanation, Constants.Preferences.TooltipAllFormatString);
            this.labelTooltip.Text = Properties.Resources.ToolTip;
            this.btnOnlineHelp.Text = Properties.Resources.btnOnlineHelp;
            this.linkLabelUseNotePad.Text = Properties.Resources.linkLabelUseNotePad;
            this.toolTip.SetToolTip(this.linkLabelUseNotePad, Properties.Resources.ttLinkLabelUseNotePad);
            this.btnRefresh.Text = CommonResources.Text.ActionRefresh;
            this.toolTip.SetToolTip(this.btnRefresh, Properties.Resources.ttbtnRefresh);
            this.linkLabelCopyFileName.Text = Properties.Resources.linkLabelCopyFileName;
            this.lblMoreSettings.Text = Properties.Resources.lblMoreSettings;
            this.btnReset.Text = Properties.Resources.Reset;
            this.toolTip.SetToolTip(this.btnReset, Properties.Resources.ttBtnReset);
            this.toolTip.SetToolTip(this.toleranceForLength, Properties.Resources.ttToleranceForLength);
            this.labelToleranceForLength.Text = Properties.Resources.labelToleranceForLength;
            this.btnReadme.Text = Properties.Resources.readmeButton;

            Refresh();
        }

        private void presentRadius()
        {
            toleranceForLength.Text = UnitUtil.Elevation.ToString(Settings.MaxRadius, "u");
        }
        private void lblLicense_Click(object sender, EventArgs e)
        {
            try
            {
                ProcessStartInfo procStartInfo = new ProcessStartInfo("http://www.gnu.org/licenses/lgpl.html");
                Process.Start(procStartInfo);
            }
            catch(Exception ex)
            {
                MessageBox.Show("Exception encountered launching browser"+ex, "Launching other application",
                                MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
        }

        private void readmeButton_Click(object sender, EventArgs e)
        {
            GenericMessageBox readmeform = new GenericMessageBox(Properties.Resources.Settings_Readme, Properties.Resources.README,
                                                                 Plugin.GetApplication().VisualTheme);
            readmeform.ShowDialog();
        }

        private void toleranceForLength_LostFocus(object sender, System.EventArgs e)
        {
            float result;
            result = (float)UnitUtil.Elevation.Parse(toleranceForLength.Text);
            if (!float.IsNaN(result) && result > 0)
            {
                Settings.MaxRadius = result;
            }
            presentRadius();
        }

        private void textBoxToolTip_TextChanged(object sender, EventArgs e)
        {
            Settings.ToolTipFormatString = txtBoxToolTip.Text;
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            _settingsController.Load();
            UpdateUIFromPreferences();
        }

        private void buttonReset_Click(object sender, EventArgs e)
        {
            DialogResult res = MessageBox.Show(Properties.Resources.Settings_ConfirmDeletePositionGroups, Properties.Resources.Reset,
                                               MessageBoxButtons.YesNoCancel);
            if (res == DialogResult.Cancel)
                return;

            _settingsController.ResetToDefaultSettings(res == DialogResult.Yes);
            UpdateUIFromPreferences();
        }

        private void linkLabelCopyFileName_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Clipboard.SetText(lblFileName.Text);
        }

        private void linkLabelUseNotePad_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                ProcessStartInfo procStartInfo = new ProcessStartInfo();
                procStartInfo.FileName = "notepad.exe";
                procStartInfo.UseShellExecute = true;
                procStartInfo.WindowStyle = ProcessWindowStyle.Normal;
                procStartInfo.Arguments = Constants.Preferences.FullFileName;
                Process.Start(procStartInfo);
                MessageBox.Show(Properties.Resources.Settings_EditInstruct + Environment.NewLine,
                                Properties.Resources.Settings_RefreshReminder,
                                MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception encountered launching default editor for xml files"+ex,
                                "Launching other application", MessageBoxButtons.OK,
                                MessageBoxIcon.Asterisk);
            }
        }

        private void buttonOnlineHelp_Click(object sender, EventArgs e)
        {
            try
            {
                ProcessStartInfo procStartInfo = new ProcessStartInfo(Properties.Resources.UrlToHelp);
                Process.Start(procStartInfo);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception encountered launching browser"+ex, "Launching other application",
                                MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
        }
    }
}